package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.UserDao;
import com.zjitc.po.User;
import com.zjitc.service.UserService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/28 13:54
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  public UserService userService;

  @RequestMapping("/test")
  @ResponseBody
  public ServerResponse test() throws ParseException {
    User user =new User();
    user.setId(1);
    user.setUsername("sda");
    user.setPassword("sfsafs");
    user.setEmail("dfsds");
    user.setPhone("sfaf");
    user.setQuestion("gdsgsg");
    user.setAnswer("aaaaa");
    user.setRole(1);
    Date epoch = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");
    //Date date=null;
    //try {
    //  date = format.parse(String.valueOf(new Date()));
    //} catch (ParseException e) {
    //  e.printStackTrace();
    //}
    user.setCreateTime(epoch);
    user.setUpdateTime(new Date());
    return ServerResponse.createSuccess(user);
  }

  @RequestMapping("/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam(name = "username",required = false)String username,
      @RequestParam(name = "password",required = false)String password,
      HttpSession session){
    ServerResponse<User> response=userService.login(username,password);
    if(response.success()){
      session.setAttribute("user",response.getData());
    }
    return response;
  }
}
